package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Tag;
import com.ruoyi.system.domain.vo.FirstPageArticle;
import com.ruoyi.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Jweb;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【后端技术】Controller
 *
 * @author ruoyi
 * @date 2022-11-30
 */
@RestController
@RequestMapping("/system/jweb")
public class JwebController extends BaseController
{
    @Autowired
    private IJwebService jwebService;

    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleService articleService;
    /**
     * 查询【后端技术】列表
     */
    @PreAuthorize("@ss.hasPermi('system:jweb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Jweb jweb)
    {
        startPage();
        List<Jweb> list = jwebService.selectJwebList(jweb.getInfoid());
        return getDataTable(list);
    }

    /**
     * 导出【后端技术】列表
     */
    @PreAuthorize("@ss.hasPermi('system:jweb:export')")
    @Log(title = "【后端技术】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jweb jweb)
    {
        List<Jweb> list = jwebService.selectJwebList(jweb.getInfoid());
        ExcelUtil<Jweb> util = new ExcelUtil<Jweb>(Jweb.class);
        util.exportExcel(response, list, "【后端技术】数据");
    }

    /**
     * 获取【后端技术】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:jweb:query')")
    @GetMapping(value = "/{jid}")
    public AjaxResult getInfo(@PathVariable("jid") Long jid)
    {return AjaxResult.success(jwebService.selectJwebByJid(jid));}
    @PreAuthorize("@ss.hasPermi('system:jweb:name')")
    @GetMapping(value = "/by/{name}")
    public TableDataInfo getInfoByName(@PathVariable("name") String name)
    {List<Jweb> jwebs = jwebService.selectJwebByName(name);return getDataTable(jwebs);}
    /**新增【后端技术】*/
    @PreAuthorize("@ss.hasPermi('system:jweb:add')")
    @Log(title = "【后端技术】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Jweb jweb)
    {Tag tag = new Tag();tag.setInfoid(2l);tag.setNames(jweb.getName());
        tagService.saveTag(tag);return toAjax(jwebService.insertJweb(jweb));}
    /** 修改【后端技术】*/
    @PreAuthorize("@ss.hasPermi('system:jweb:edit')")
    @Log(title = "【后端技术】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Jweb jweb)
    {Jweb jweb1 = jwebService.selectJwebByJid(jweb.getJid());
        Tag tagByName = tagService.getTagByName(jweb1.getName());
        tagByName.setNames(jweb.getName());
        tagService.updateTag(tagByName);return toAjax(jwebService.updateJweb(jweb));}
    /**删除【后端技术】*/
    @PreAuthorize("@ss.hasPermi('system:jweb:remove')")
    @Log(title = "【后端技术】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{jids}")
    public AjaxResult remove(@PathVariable Long[] jids)
    {for(Long jid : jids){
            Jweb jweb = jwebService.selectJwebByJid(jid);
            Tag tagByName = tagService.getTagByName(jweb.getName());
        List<FirstPageArticle> articleTagId = articleService.getArticleTagId(tagByName.getId());
        for(FirstPageArticle firstPageArticle : articleTagId){
            articleService.deleteArticle(firstPageArticle.getId());
        }
        tagService.deleteTag(tagByName.getId());
    }return toAjax(jwebService.deleteJwebByJids(jids));}
}
